package com.portal.service.impl.system;

import java.util.ArrayList;
import java.util.List;

import com.portal.model.system.Menu;
import com.portal.service.impl.BaseService;
import com.portal.service.system.MenuService;
import com.util.portal.PageConfig;



public class MenuServiceImpl extends BaseService implements MenuService {

	public List<Menu> findMenu() {
		/**
		 * 得到第一层的菜单
		 */
		Menu menu = new Menu();
		menu.setIsValid("1");
		menu.setMLevel(1);
		List<Menu> m1 = this.getSysMenuDao().getList(menu);
		
		/**
		 * 得到第二层的菜单
		 */
		menu.setIsValid("1");
		menu.setMLevel(2);
		List<Menu> m2 = this.getSysMenuDao().getList(menu);
		
		ArrayList<Menu> menuList = new ArrayList<Menu>();
		for(Menu menu1 :m1){
			ArrayList<Menu> childrenList = new ArrayList<Menu>();
			for(Menu menu2 :m2){
				if(menu1.getMenuCode().equals(menu2.getParentMenuCode())){
					childrenList.add(menu2);
				}
			}
			
			if(childrenList.size()>0){
				menu1.setChildren(childrenList);
			}
			menuList.add(menu1);
		}
		return menuList;
	}

	public List<Menu> getList(Menu menu, PageConfig pageConfig) {
		if (pageConfig != null) {
			Integer rowcount = this.getSysMenuDao().getRowCount(menu);
			pageConfig.setTotalCount(rowcount);
			return this.getSysMenuDao().getList(menu,
					this.getRowBounds(pageConfig));
		} else {
			return this.getSysMenuDao().getList(menu);
		}
	}

	public Integer getRowCount(Menu t) {
		return this.getSysMenuDao().getRowCount(t);
	}

	public Menu read(String id) {
		return this.getSysMenuDao().read(id);
	}

}
